package cn.hanna.springcloud.controller;

import cn.hanna.springcloud.entities.CommonResult;
import cn.hanna.springcloud.entities.Payment;
import cn.hanna.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author smileha
 * @create 2021-10-16 13:29
 * @description
 */
@RestController
@Slf4j
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("****输出结果****："+result);
        if(result > 0){
            return new CommonResult<>(200,"插入数据成功",payment);
        }else{
            return new CommonResult<>(444,"插入数据数据失败",null);
        }
    }

    @GetMapping("/payment/get/{id}")
    public CommonResult getSerialById(@PathVariable("id") Long id){
        Payment payment = paymentService.getSerialById(id);
        if(payment != null) {
            return new CommonResult<>(200,"获取数据成功,访问端口为:"+serverPort,payment);
        }else{
            return new CommonResult<>(444,"未能找到相应的数据，ID:"+id+",访问端口为:"+serverPort,null);
        }
    }

    // 获取本机端口号,测试自定义负载均衡功能
    @GetMapping("/payment/loadbalance")
    public String getLB(){
        return serverPort;
    }

    // 检验openfeign的超时控制
    @GetMapping("/payment/outtime")
    public String checkOutTime() throws InterruptedException {
        Thread.sleep(3000);
        return serverPort;
    }

    // 检验sleuth的链路监控
    @GetMapping("/payment/zipkin")
    public String checkSleuth(){
        return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
    }
}
